home *** CD-ROM | disk | FTP | other *** search
- Path: diku.dk!null
- From: null@diku.dk (Niels Ull Jacobsen)
- Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.tools.mfc
- Subject: Re: Newbie question: default op=
- Date: 28 Jan 1996 12:13:40 GMT
- Organization: Department of Computer Science, U of Copenhagen
- Sender: null@tyr.diku.dk
- Message-ID: <4efp9k$3el@odin.diku.dk>
- References: <30FEA0B4.4F66@ymi.com> <4ebacl$ca4@waena.maui.com>
- NNTP-Posting-Host: odin.diku.dk
- X-Newsreader: NN version 6.5.0 #13
-
- "Paul A. Billings" <pab@maui.com> writes:
-
- >Jeffrey Keays <keays@ymi.com> wrote:
- >>I am trying to use the &*%$ MFC collections in some way that will dispose of its contents on destruction. In
- >>my 2-days and counting of ramming my head repeatedly against this brick wall over something that should be made
- >>'easier' by them, I have this general C++ question.
-
- The simple MFC collections (CObList and so on) *will* dispose of their
- contents on destruction. All they keep are *pointers* to the elements,
- not the elements themselves, and these pointers are properly disposed
- of :-).
-
- But if you want to have collections with this behaviour, it should be
- trivial to add you own, derived from the existing ones.
-
- For the templated collections, it's likewise trivial to derive your own.
-
- Remember what the F in MFC stands for - MFC is *not* supposed to be
- the final do-it-all library. And collection classes is a particularly
- weak field of MFC.
-
- >>
- >>Why is it making me create operator= functions for assignment of the same type (or instance / reference of the
- >>same type). The class I'm trying to assign has no pointers or anything that would require special handling.
- >>Why do I have to write stupid things like
- >>
- >>class foo {
- >> int data1;
- >> double data2;
- >> CString data3;
- >> // ... ad infinitum
- >>public:
- >> foo& operator=(const foo &);
- >>}
- >>
- >>foo& foo::operator=(const foo &rhs)
- >>{
- >> data1 = rhs.data1
- >> data2 = rhs.data2
- >> data3 = rhs.data3
- >> // ... -- I should not have to write this, it should be ASSUMED
- >> // by the compiler if I don't specify otherwise.
- >>}
-
-
- >Relying on any compiler bit-wise copy is OK if using elemental
- >types, but as soon as you threw CString in there, you
- >complicated things. You don't want to copy the CString, you
- >want to call CString::operator =(). Sooo, form that
- >standpoint, you *should* provide =().
-
-
- >> joblist.AddTail(Job(p, wfd.cFileName));
- >>This is the line in MFC with the complaint,
- >> pNewNode->data = newElement;
- >>
- >>F:\MSDEV\MFC\include\afxtempl.h(813) : error C2582: 'Job' : 'operator =' function is unavailable
-
- >As to why the compiler complains, I'm unsure why it does. It's
- >supposed to use the default =() if none are specified. I've
- >been burned by this because often the bit-wise copy is not what
- >you want. Sorry no help here.
-
- CObject has a private operator= (and a private copy constructor)
- specifically to avoid this common pitfall. You should *always* write a
- copy constructor and/or an operator= if you want to use it. Relying on
- the default bitwise copy is *very* dangereous, e.g. for CStrings.
-
-
- >Paul
-
-
-
-
-
-
-
-
- --
- Niels Ull Jacobsen, Dep. of CS, U of Copenhagen (null@diku.dk)
- Roenne Alle 3 st.th, 2860 Soeborg, Denmark, tel. +45 39 66 39 86
-
-